package com.atwy.search;

/**
 * 线性查找
 * 无序有序都行，挨个比较
 */
public class LinearSearch {
    public static void main(String[] args) {
        int[] array = {3, 65, 2, 34, 12, 5, 9};
        int index = search(array, 12);

        if (index == -1) {
            System.out.println("没有找到到");
        } else {
            System.out.println("找到，下标为=" + index);
        }
    }


    public static int search(int[] array, int target) {
        if (array == null || array.length < 1) {
            return -1;
        }
        for (int i = 0; i < array.length; i++) {
            if (target == array[i]) {
                return i;
            }
        }

        return -1;
    }
}
